<?php
namespace app\index\model;
use think\Model;
class Orders extends Model
{
	public function index(){
		$data['uid'] = session('uid');
        if(empty($data['uid'])){
           $data['uid'] = input('param.uid');
        }
		$data['codeifd'] = date('Ymd') . str_pad(mt_rand(1, 99999999), 5, '0');
		$data['qishu'] = input('param.qishu');
        $data['money'] = input('param.money');
        $buy = input('param.updown');
        $data['updown'] = $buy;
        $data['ordertime'] = date('Y-m-d H:i:s');
    /*    return $data;*/
        $order = db('users')->field('uid,username,pid,money,vip,return')->where('uid',$data['uid'])->find();
        $otime = time();
        $kai = db('result')->where('qishu',$data['qishu'])->find();
        $feng = strtotime($kai['time'])-120;
        if($data['money']>$order['money']){
            //账户余额不足
            /*return 101;*/
            return ['code'=>0,'msg'=>'账户余额不足'];
        }
        if($otime > $feng){
            return ['code'=>0,'msg'=>'已经封盘不能进行下注'];
        }
        //返回自己的金额
        $return = $order['return']*$data['money'];
        $userself = array(
                'uid'=>$data['uid'],
                'pid'=>$data['uid'],
                'users'=>$order['username'],
                'money'=>$return,
                'createtimes'=>date('Y-m-d H:i:s'),
        );
        if($order['pid'] == 0){
           $datas=db('order')->insertGetId($data);
           if($datas){
              db('users')->where('uid',$data['uid'])->setDec('money',$data['money']);
              if($order['vip']>1){
                   db('users')->where('uid',$data['uid'])->setInc('money',$return);
                   db('usermoneylog')->insert($userself);
                }
               /*  db('usermoneylog')->insert($usermoney);*/
                 return ['code'=>1,'msg'=>'下注成功'];
           }else{
              return ['code'=>0,'msg'=>'下注失败'];
           }
        }else{
        $orders = db('users')->field('username,return')->where('uid',$order['pid'])->find();
        //返回推荐人的金额
        $returns = ($orders['return']-$order['return'])*$data['money'];
        $usermoney = array(
                'uid'=>$data['uid'],
                'pid'=>$order['pid'],
                'users'=>$orders['username'],
                'money'=>$returns,
                'createtimes'=>date('Y-m-d H:i:s'),

        );
             $datas=db('order')->insertGetId($data);
             if($datas){
                db('users')->where('uid',$data['uid'])->setDec('money',$data['money']);
                db('users')->where('uid',$order['pid'])->setInc('money',$returns);
                if($order['vip']>1){
                   db('users')->where('uid',$data['uid'])->setInc('money',$return);
                   db('usermoneylog')->insert($userself);
                }
                 db('usermoneylog')->insert($usermoney);
                 return ['code'=>1,'msg'=>'下注成功'];
             }else{
                //下注失败
                return ['code'=>0,'msg'=>'下注失败'];
             }
        }     
	}
}